﻿using HNCSolutions.Models;
using HNCSolutions.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HNCSolutions.Areas.Admin.Controllers
{
    public class SanPhamController : Controller
    {
        #region Fields
        private HNCSolutionsEntities db = new HNCSolutionsEntities();
        #endregion

        public SelectList LayNhomSanPham()
        {
            SelectList nhomSanPham = new SelectList(db.NhomSanPhams, "NhomSanPhamId", "TenNhom");
            return nhomSanPham;
        }

        public JsonResult GetCustomers()
        {
            var customers = db.Menus.ToList();

            List<object> dropDownInfos = new List<object>();
            customers.ForEach(t =>
            {
                dropDownInfos.Add(new
                                                    {
                                                        id = t.Id,
                                                        label = t.Text,
                                                        value = t.URL,
                                                    });
            });

            return Json(dropDownInfos, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Admin/SanPham/
        public ActionResult Index(int currentPage = 1, int pageRows = 10)
        {
            var listSanPham = db.SanPhams.OrderBy(sp => sp.SanPhamId).Skip((currentPage - 1) * pageRows).Take(pageRows).ToList();
            int totalPages = (db.SanPhams.Count() / pageRows) + (db.SanPhams.Count() % pageRows > 0 ? 1 : 0);
            ListSanPhamViewModel viewModel = new ListSanPhamViewModel
            {
                ListSanPham = listSanPham,
                Paging = new Paging { CurrentPage = currentPage, PageRows = pageRows, TotalPage = totalPages }
            };

            return View(viewModel);
        }

        public ActionResult Submit(string submitForm, IEnumerable<int> intDeletedIds)
        {
            var sanPhams = this.db.SanPhams.Where(sp => intDeletedIds.Contains(sp.SanPhamId)).ToList();
            sanPhams.ForEach(sp => this.db.SanPhams.Remove(sp));
            this.db.SaveChanges();
            return RedirectToAction("Index");
        }

        #region Create
        //
        // GET: /Admin/SanPham/Create
        public ActionResult Create()
        {
            SanPhamViewModel viewModel = new SanPhamViewModel { NhomSanPhamList = this.LayNhomSanPham() };
            ModelState.Remove("NhomSanPhamList");
            return View(viewModel);
        }

        //
        // POST: /Admin/SanPham/Create
        [HttpPost]
        public ActionResult Create(SanPhamViewModel viewModel)
        {
            SanPham sanpham = viewModel.SanPham;
            var fileName = Path.GetFileName(sanpham.File.FileName);
            sanpham.HinhAnh = fileName;
            var path = Path.Combine(Server.MapPath("~/Images/Upload"), fileName);
            sanpham.File.SaveAs(path);
            db.SanPhams.Add(sanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/SanPham/Edit/5
        public ActionResult Edit(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            ViewBag.NhomSanPham = this.LayNhomSanPham();
            if (sanpham == null)
            {
                return HttpNotFound();
            }

            return View(sanpham);
        }

        //
        // POST: /Admin/SanPham/Edit/5
        [HttpPost]
        public ActionResult Edit(SanPham sanpham, string buttonPost)
        {
            if (buttonPost == "List")
            {
                return this.RedirectToAction("Index");
            }

            if (ModelState.IsValid)
            {
                db.Entry(sanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(sanpham);
        }
        #endregion

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}